//
//  CircleLayer.swift
//  QBLaunchAnimation
//
//  Created by 乐启榜 on 16/8/26.
//  Copyright © 2016年 乐启榜. All rights reserved.
//

import UIKit

class CircleLayer: CAShapeLayer {
    
    let kAnimationDuration: CFTimeInterval = 0.3
    let kAnimationBeginTime: CFTimeInterval = 0.0
    
    override init() {
        super.init()
        self.fillColor = UIColor.colorWithHexString("#da70d6").CGColor
        self.path = circleSmallPath.CGPath
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var circleSmallPath: UIBezierPath {
        return UIBezierPath(ovalInRect: CGRect(x: 50, y: 50, width: 0, height: 0))
    }
    var circleBigPath: UIBezierPath {
        return UIBezierPath(ovalInRect: CGRect(x: 2.5, y: 17.5, width: 95, height: 95))
    }
    
    var circleVerticalSquishPath: UIBezierPath {
        return UIBezierPath(ovalInRect: CGRect(x: 2.5, y: 20.0, width: 95.0, height: 90.0))
    }
    
    var circleHorizontalSquishPath: UIBezierPath {
        return UIBezierPath(ovalInRect: CGRect(x: 5.0, y: 20.0, width: 90.0, height: 90.0))
    }
    
    // 1、扩大动画
    func expand() {
        let expandAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
        expandAnimation.fromValue = circleSmallPath.CGPath
        expandAnimation.toValue = circleBigPath.CGPath
        expandAnimation.duration = kAnimationDuration
        expandAnimation.fillMode = kCAFillModeForwards
        expandAnimation.removedOnCompletion = false
        self.addAnimation(expandAnimation, forKey: nil)
    }
    
    // 2、压扁动画
    func wobbleAnimation() {
        
        // 1 animation begin from bigPath to verticalPath
        let wobbleAnimation1: CABasicAnimation = CABasicAnimation(keyPath: "path")
        wobbleAnimation1.fromValue = circleBigPath.CGPath
        wobbleAnimation1.toValue = circleVerticalSquishPath.CGPath
        wobbleAnimation1.beginTime = kAnimationBeginTime
        wobbleAnimation1.duration = kAnimationDuration
        
        // 2 animation vertical to horizontal
        let wobbleAnimation2: CABasicAnimation = CABasicAnimation(keyPath: "path")
        wobbleAnimation2.fromValue = circleVerticalSquishPath.CGPath
        wobbleAnimation2.toValue = circleHorizontalSquishPath.CGPath
        wobbleAnimation2.beginTime = wobbleAnimation1.beginTime + wobbleAnimation1.duration
        wobbleAnimation2.duration = kAnimationDuration
        
        // 3 animation horizontal to vertical
        let wobbleAnimation3: CABasicAnimation = CABasicAnimation(keyPath: "path")
        wobbleAnimation3.fromValue = circleHorizontalSquishPath.CGPath
        wobbleAnimation3.toValue = circleVerticalSquishPath.CGPath
        wobbleAnimation3.beginTime = wobbleAnimation2.beginTime + wobbleAnimation2.duration
        wobbleAnimation3.duration = kAnimationDuration
        
        // 4 animation vertical to beginPath
        let wobbleAnimation4: CABasicAnimation = CABasicAnimation(keyPath: "path")
        wobbleAnimation4.fromValue = circleVerticalSquishPath.CGPath
        wobbleAnimation4.toValue = circleBigPath.CGPath
        wobbleAnimation4.beginTime = wobbleAnimation3.beginTime + wobbleAnimation3.duration
        wobbleAnimation4.duration = kAnimationDuration
        
        // 5、animationGroup
        let wobbleAnimationGroup: CAAnimationGroup = CAAnimationGroup()
        wobbleAnimationGroup.animations = [wobbleAnimation1,wobbleAnimation2,wobbleAnimation3,wobbleAnimation4]
        wobbleAnimationGroup.duration = 4 * kAnimationDuration
        wobbleAnimationGroup.repeatCount = 2
        addAnimation(wobbleAnimationGroup, forKey: nil)
    }
    
    func contract() {
        let contractAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
        contractAnimation.fromValue = circleBigPath.CGPath
        contractAnimation.toValue = circleSmallPath.CGPath
        contractAnimation.duration = 0.6
        contractAnimation.fillMode = kCAFillModeForwards
        contractAnimation.removedOnCompletion = false
        self.addAnimation(contractAnimation, forKey: nil)
    }

}
